package com.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by CrazyShaQiuShi on 2017/1/25.
 */
public enum  SingltonCglibProxy implements MethodInterceptor{
    INSTANCE {
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            before();
            Object object=methodProxy.invokeSuper(o,objects);
            after();
            return object;
        }

    };
    private SingltonCglibProxy(){}
    protected void before(){
        System.out.println("ARE YOU OK?");
    }
    protected void after(){
        System.out.println("NO");
    }
    public <T>T getProxy(Class<T> clazz){
        return (T) Enhancer.create(clazz,this);
    }
}
